<template>
	<view class="myInfo">
		<uni-nav-bar color="#ffffff" background-color="#00aded" :status-bar="true" title="我的" fixed="true" />
		<view class="myInfoTop">
			<view class="topLeft">
				<image class="img" src="../../static/info/log.png"></image>
			</view>
			<view class="topRight">
				<view class="myInfoTopUserName">
					<text>{{ userinfoData.userName }}</text>
				</view>
				<view class="myInfoTopUserInfo">
					账号：
					<text>{{ userinfoData.userCode }}</text>
				</view>
				<view>
					机构：
					<text>{{ orgName }}</text>
				</view>
				<view class="myInfoTopUserInfo">
					岗位：
					<text>{{ dutyName }}</text>
				</view>
			</view>
		</view>
		<view class="myInfoMain">
			<uni-list>
				<uni-list-item
					v-for="(item, index) in menuList"
					:key="index"
					:title="item.menuLabel"
					show-arrow="true"
					showExtraIcon="true"
					:extra-icon="item.extraIcon"
					:clickable="true"
					@click="goNav(item)"
				></uni-list-item>
			</uni-list>
		</view>
		<view class="myInfloot">
			<uni-popup ref="popup" type="center">
				<uni-popup-dialog
					mode="base"
					message="成功消息"
					content="退出后不会清空任何历史数据,下次登录依然可以使用本账号."
					:duration="2000"
					:before-close="true"
					@close="close"
					@confirm="confirm"
				></uni-popup-dialog>
			</uni-popup>
			<button type="default" @click="quitLogin">退出登录</button>
		</view>
	</view>
</template>

<script>
import { getMenu } from '@/utils/getMenu.js';
export default {
	data() {
		return {
			menuList: [],
			userinfoData: null,
			dictDataArr: [],
			roleName: [],
			orgId: '',
			orgName: '',
			orgCode: '',
			depTypeCode: '',
			dutyName: '',
			dutyId: ''
		};
	},
	onShow() {
		this.getInfoTop();
		let menuList = [];
		getMenu({
			type: '我的'
		}).then((res) => {
			console.log('res: ', res);
			this.menuList = res;
		});
	},
	methods: {
		getInfoTop() {
			let _this = this;
			try {
				let userInfo = JSON.parse(uni.getStorageSync('userInfo') || '{}');
				if (userInfo) {
					this.userinfoData = userInfo || '';
				}
			} catch (e) {}
			_this.userInfo = JSON.parse(uni.getStorageSync('userInfo') || '{}');
			_this.orgId = JSON.parse(uni.getStorageSync('orgId') || 'null') || '';
			_this.orgName = JSON.parse(uni.getStorageSync('orgName') || 'null') || '';
			_this.dutyId = JSON.parse(uni.getStorageSync('dutyId') || 'null') || '';
			_this.depTypeCode = JSON.parse(uni.getStorageSync('depTypeCode') || 'null') || '';
			_this.dutyName = JSON.parse(uni.getStorageSync('dutyName') || 'null') || '';
		},
		async goNav(item) {
			uni.setStorageSync('activeMenu', item.menuId);
			let appId = uni.getStorageSync('appId');
			let companyId = uni.getStorageSync('companyId');
			const res = await this.$mySsoRequest({
				url:
					'/icore.icp.web/pass/v1/sysmenus/findFunctionMenusByUserId?companyId=' +
					companyId +
					'&appId=' +
					appId +
					'&userId=' +
					uni.getStorageSync('userId') +
					'&menuType=2' +
					'&pId=' +
					item.menuId
			}).catch((e) => {
				uni.showToast({
					duration: 2000,
					title: e.message,
					icon: 'none'
				});
			});
			let arr = res.data.data
				.filter((e) => {
					return e.pId == item.menuId;
				})
				.map((e) => {
					return e.privilege;
				});
			uni.setStorageSync('preHiddenData', JSON.stringify(arr));
			uni.navigateTo({
				url: item.menuUrl
			});
		},
		quitLogin() {
			this.$refs.popup.open();
		},
		close() {
			this.$refs.popup.close();
		},
		confirm() {
			this.$refs.popup.close();
			this.bindCid();
			uni.removeStorageSync('userInfo');
			uni.removeStorageSync('token');
			uni.removeStorageSync('header');
			uni.redirectTo({
				url: '/pages/login/login'
			});
		},
		// 绑定账号与Cid
		async bindCid() {
			let userId = uni.getStorageSync('userId');
			let cid = uni.getStorageSync('getui_cid');
			const res = await this.$myRequest({
				url: '/api/v1/bp/insertAppCid',
				data: {
					userCode: this.userName,
					cid: cid,
					insertUsername: userId,
					delete: 1
				}
			}).catch((err) => {
				console.error(err);
			});
			console.log(res, 'res');
		}
	},
	created() {}
};
</script>

<style lang="scss" scoped>
.myInfo {
	display: flex;
	flex-direction: column;
	width: 100%;
	height: 100%;
	background-color: #f5f5f5;
	.myInfoTop {
		background-color: #fff;
		height: 160px;
		display: flex;
		flex-direction: row;
		// justify-content: center;
		align-items: center;
		.topLeft {
			margin-left: 20rpx;
			.img {
				width: 200rpx;
				height: 200rpx;
				border-radius: 200rpx;
			}
		}
		.topRight {
			margin-left: 40rpx;
			.myInfoTopUserName {
				font-weight: 700;
				font-size: 36rpx;
				margin-bottom: 10rpx;
			}
			.myInfoTopUserInfo {
				margin-bottom: 10rpx;
				margin-top: 10rpx;
			}
		}
	}
	.myInfoMain {
		margin-top: 20rpx;
	}
	.myInfloot {
		margin-top: 16rpx;
		uni-button {
			background-color: #fff;
		}
	}
}
</style>